package com.forum.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.forum.business.ForumManager;
import com.forum.model.dao.ForumUserDAO;
import com.forum.model.pojo.ForumUser;

@WebServlet("/RegisterController")
public class RegisterController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ForumUser user = ForumManager.fillUserData(request);
		ForumUserDAO userDao = new ForumUserDAO();
		String path = "/index.jsp";
		
		if (userDao.insert(user) == false){
			request.setAttribute("message", "Registration failed, username already exist!");
			path = "/register.jsp";
		}
		
		RequestDispatcher dispatcher = request.getRequestDispatcher(path);
		if (dispatcher != null) dispatcher.forward(request, response);
	}
}